package top.sxlai.controller;
import top.sxlai.util.CurrentSystemTime;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class HdfsController {

    private static final Logger log = LoggerFactory.getLogger(HdfsController.class);
    @Autowired
    private FileSystem fileSystem;

//    @GetMapping("/hdfs/file/preview")
//    public ResponseEntity<String> previewHdfsFile(@RequestParam("path") String filePath) {
//        try {
//            log.info("开始返回预览数据:{}", CurrentSystemTime.getTime());
//            Path hdfsPath = new Path(filePath);
//            InputStream inputStream = fileSystem.open(hdfsPath);
//            byte[] fileContent = IOUtils.toByteArray(inputStream);
//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//            String blobUrl = "data:audio/mpeg;base64," + Base64.getEncoder().encodeToString(fileContent);
//            return ResponseEntity.ok()
//                    .header("Content-Disposition", "inline")
//                    .body(blobUrl);
//        } catch (IOException e) {
//            log.error("打开或读取HDFS文件出错", e);
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//        }
//
//    }

    @GetMapping("/hdfs/file/preview")
    public ResponseEntity<Resource> previewHdfsFile(@RequestParam("path") String filePath,@RequestParam("typeId")int typeId) {
        try {
            log.info("开始返回预览数据:{}", CurrentSystemTime.getTime());
            Path hdfsPath = new Path(filePath);
            InputStream inputStream = fileSystem.open(hdfsPath);
            HttpHeaders headers = new HttpHeaders();
            if(typeId==2){
                //如果id为2设置为视频类型
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            }else if(typeId==3){
                //如果为3设置为音频类型
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            }else if(typeId==4){
                //图片类型
                headers.setContentType(MediaType.IMAGE_JPEG);
            }else if(typeId==5){
                //pdf类型
                headers.setContentType(MediaType.APPLICATION_PDF);
            }else if(typeId==6){
                // txt类型
                headers.setContentType(MediaType.TEXT_PLAIN);
            }else if(typeId==7){
                //word类型
                headers.setContentType(MediaType.valueOf("application/msword"));
            }
            headers.setContentDisposition(ContentDisposition.builder("inline").filename(filePath).build());
            InputStreamResource resource = new InputStreamResource(inputStream);
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } catch (IOException e) {
            log.error("打开或读取HDFS文件出错", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }


}